//
//  HomeController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/9/29.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "HomeController.h"
#import "SCScanController.h"
#import "HomeButton.h"
#import "HomeCentenView.h"
#import "HomeBottomView.h"

#import "MineViewController.h"
#import "BillViewController.h"
#import "VerificationCouponController.h"
#define kMineHeaderH  kWidth*(400/750.0)

typedef enum : NSUInteger {
    ButtonTotol,
    ButtonAlready,
    ButtonDestroy,
} HomeButtonType;

@interface HomeController ()

@property (nonatomic,weak)UIImageView     *headV;

@property (nonatomic,weak)HomeButton *homeLBtn;
@property (nonatomic,weak)HomeButton *homeCBtn;
@property (nonatomic,weak)HomeButton *homeRBtn;

@property (nonatomic,weak)HomeCentenView *CententV;

@property (nonatomic,weak)HomeBottomView *bottomV;

@property (nonatomic,strong)SCUser *user;

@end

@implementation HomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"黄牛派商户版";
    
    [SCTool checkAppUpdate];
    [self setUI];
}
- (void)loadData{
    
    [SCApiTool ScStoredetailSuccess:^(NSDictionary *data) {
        
        [SCMYModel GetMineData:data];
        [_headV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[data hw_safeObjectForKey:@"store_banner"]]] placeholderImage:[UIImage imageNamed:@"home_back"]];
        
        
        if (![self.user.joinin_state isEqualToString:@"20"]) {
            _homeLBtn.numL.text = @"0";
            _homeCBtn.numL.text = @"0";
            _homeRBtn.numL.text = @"0";
        }else{
            _homeLBtn.numL.text = self.user.credit_total;
            _homeCBtn.numL.text = self.user.credit_coupon_assigned;
            _homeRBtn.numL.text = self.user.credit_coupon_consumed;
        }
        _CententV.user = self.user;
        _bottomV.user = self.user;
    } failure:^(NSString *errormsg) {
        [SCTool showError:errormsg];
    }];
}
- (void)NavItem{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 80, 32);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button setTitle:@"扫一扫" forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"code"] forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -32-4, 0, 0)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 48+8, 0,0 )];
    [button addTarget:self action:@selector(codeClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
}
- (void)codeClick{
    SCScanController *scanVC = [[SCScanController alloc] init];
    [self.navigationController pushViewController:scanVC animated:YES];
}
- (void)setUI{
    
    [self NavItem];
    
    UIScrollView *mainView = [[UIScrollView alloc]init];
    mainView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:mainView];
    UIView *containerV = [[UIView alloc]init];
    [mainView addSubview:containerV];
    
    UIImageView *headerV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kMineHeaderH)];
    headerV.userInteractionEnabled = YES;
    headerV.image = [UIImage imageNamed:@"home_back"];
//    headerV.contentMode=UIViewContentModeScaleAspectFill;
    headerV.autoresizesSubviews = YES;
    [containerV addSubview:headerV];
    _headV = headerV;

    UIView *btnBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 70)];
    btnBg.backgroundColor = [UIColor whiteColor];
    [containerV addSubview:btnBg];
    
    HomeButton *homeLBtn = [[HomeButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 70)];
    homeLBtn.titleL.text = @"店铺总额度";
    [homeLBtn addTarget:self action:@selector(headerClcik:) forControlEvents:UIControlEventTouchUpInside];
    homeLBtn.tag = ButtonTotol;
    [btnBg addSubview:homeLBtn];
    _homeLBtn = homeLBtn;
    
    HomeButton *homeCBtn = [[HomeButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 70)];
    homeCBtn.titleL.text = @"已分配劵额度";
    [homeCBtn addTarget:self action:@selector(headerClcik:) forControlEvents:UIControlEventTouchUpInside];
    homeCBtn.tag = ButtonAlready;
    [btnBg addSubview:homeCBtn];
    _homeCBtn = homeCBtn;
    
    HomeButton *homeRBtn = [[HomeButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 70)];
    homeRBtn.titleL.text = @"已核销额度";
    [homeRBtn addTarget:self action:@selector(headerClcik:) forControlEvents:UIControlEventTouchUpInside];
    homeRBtn.tag = ButtonDestroy;
    [btnBg addSubview:homeRBtn];
    _homeRBtn = homeRBtn;
    
    HomeCentenView *CententV = [[HomeCentenView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 142) option:^(BtnClick type) {
        
        switch (type) {
            case BtnMy:
            {
                MineViewController *mineVC = [[MineViewController alloc] init];
                [self.navigationController pushViewController:mineVC animated:YES];
            }
                break;
            case BtnBill:
            {
                if (![self.user.joinin_state isEqualToString:@"20"]) {
                    [SCTool showMessage:@"您还未通过审核"];
                    return ;
                }
                BillViewController *billVC = [[BillViewController alloc] init];
                [self.navigationController pushViewController:billVC animated:YES];
            }
                break;
                case BtnWrite:
            {
                if (![self.user.joinin_state isEqualToString:@"20"]) {
                    [SCTool showMessage:@"您还未通过审核"];
                    return ;
                }
                VerificationCouponController *couponVC = [[VerificationCouponController alloc] init];
                [self.navigationController pushViewController:couponVC animated:YES];
            }
                break;
            default:
                break;
        }
    }];
    [containerV addSubview:CententV];
    _CententV = CententV;
    
    HomeBottomView *bottomV = [[[NSBundle mainBundle] loadNibNamed:@"HomeBottomView" owner:self options:nil] lastObject];
    [containerV addSubview:bottomV];
    _bottomV = bottomV;
    
    __weak __typeof(&*self)weakSelf = self;
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    
    [containerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(mainView);
        make.width.equalTo(mainView);
    }];
    
    [headerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(containerV);
        make.height.mas_equalTo(kMineHeaderH);
    }];
    [btnBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(containerV);
        make.top.equalTo(headerV.mas_bottom);
        make.height.equalTo(@70);
    }];
    NSMutableArray *btnArr = [NSMutableArray array];
    [btnArr addObjectsFromArray:@[homeLBtn,homeCBtn,homeRBtn]];
    [HWCommon makeEqualWidthViews:btnArr inView:btnBg LRpadding:0 viewPadding:0];
    
    [CententV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnBg.mas_bottom).offset(1);
        make.left.right.equalTo(containerV);
        make.size.mas_equalTo(CententV.frame.size);
    }];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(CententV.mas_bottom).offset(6);
        make.left.right.equalTo(containerV);
        make.height.equalTo(@180);
    }];
    [containerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bottomV.mas_bottom);
    }];
}
- (void)headerClcik:(UIButton *)btn{
    
    switch (btn.tag) {
        case ButtonTotol:
        {
            DLog(@"全部");
        }
            break;
        case ButtonAlready:
        {
            DLog(@"已分配");
        }
            break;
        case ButtonDestroy:
        {
            DLog(@"已销毁");
        }
            break;
            
        default:
            break;
    }
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self loadData];
   
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];

    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: KOrange,NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
}
- (SCUser *)user{
    if (_user == nil) {
        _user = [SCUserTool loadInfo];
    }
    return _user;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
